﻿using System;
using System.Collections.Generic;
using ExtAspNet;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace WebUI.LabManage
{
    public partial class LabEquipment : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
                BindForm();
                InitForm();
                bnNew.OnClientClick = window.GetShowReference("LabEquipmentEdit.aspx?action=new", "新增设备");
            }
            if (Request["__EVENTARGUMENT"] == "refreshParent")
            {
                BindGrid();
            }
        }

        private void BindForm()
        {
            ExtAspNet.ListItem item = new ExtAspNet.ListItem("全部", "-1");
            dl_status.DataSource = BLL.LabEquipment.GetParamList(5);
            dl_status.DataTextField = "value";
            dl_status.DataValueField = "id";
            dl_status.DataBind();
            dl_status.Items.Insert(0,item);
            dl_type.DataSource = BLL.LabEquipment.GetParamList(6);
            dl_type.DataTextField = "value";
            dl_type.DataValueField = "id";
            dl_type.DataBind();
            dl_type.Items.Insert(0,item);
            dl_lab.DataSource = BLL.LabEquipment.GetLabList();
            dl_lab.DataTextField = "LabName";
            dl_lab.DataValueField = "LabID";
            dl_lab.DataBind();
            dl_lab.Items.Insert(0,item);
        }

        private void InitForm()
        {
            tb_name.Text = "";
            tb_pNO.Text = "";
            tb_saller.Text = "";
            dl_lab.SelectedValue = "-1";
            dl_status.SelectedValue = "-1";
            dl_type.SelectedValue = "-1";
        }

        private void BindGrid()
        {
            Grid1.DataSource = BLL.LabEquipment.GetLabEquipmentList();
            Grid1.DataBind();
        }

        protected void bnSearch_Click(object sender, EventArgs e)
        {
            StringBuilder strWhere = new StringBuilder();
            if (tb_name.Text.Trim() != "")
            {
                strWhere.AppendFormat(" Name LIKE '%{0}%'", tb_name.Text.Trim());
            }
            if (tb_pNO.Text.Trim() != "")
            {
                if (tb_name.Text.Trim() != "")
                {
                    strWhere.Append(" AND");
                }
                strWhere.AppendFormat(" propertyNO LIKE '%{0}%'", tb_pNO.Text.Trim());
            }
            if (tb_saller.Text.Trim() != "")
            {
                if (tb_name.Text.Trim() != "" || tb_pNO.Text.Trim() != "")
                {
                    strWhere.Append(" AND");
                }
                strWhere.AppendFormat(" seller LIKE '%{0}%'", tb_saller.Text.Trim());
            }
            if(dl_type.SelectedValue != "-1")
            {
                if (tb_name.Text.Trim() != "" || tb_pNO.Text.Trim() != "" || tb_saller.Text.Trim() != "")
                {
                    strWhere.Append(" AND");
                }
                strWhere.AppendFormat(" type = {0}", dl_type.SelectedValue);
            }
            if(dl_status.SelectedValue != "-1")
            {
                if (tb_name.Text.Trim() != "" || tb_pNO.Text.Trim() != "" || tb_saller.Text.Trim() != "" || dl_type.SelectedValue != "-1")
                {
                    strWhere.Append(" AND");
                }
                strWhere.AppendFormat(" status = {0}", dl_status.SelectedValue);
            }
            if (dl_lab.SelectedValue != "-1")
            {
                if (tb_name.Text.Trim() != "" || tb_pNO.Text.Trim() != "" || tb_saller.Text.Trim() != "" || dl_type.SelectedValue != "-1" || dl_status.SelectedValue != "-1")
                {
                    strWhere.Append(" AND");
                }
                strWhere.AppendFormat(" LabID = {0}", dl_lab.SelectedValue);
            }
            Grid1.DataSource = BLL.LabEquipment.GetLabEquipmentList(strWhere.ToString());
            Grid1.DataBind();
        }

        protected void bnReset_Click(object sender, EventArgs e)
        {
            BindGrid();
            InitForm();
        }

        protected void Grid1_PageIndexChange(object sender, ExtAspNet.GridPageEventArgs e)
        {
            Grid1.PageIndex = e.NewPageIndex;
        }
        protected void Grid1_RowClick(object sender, ExtAspNet.GridRowClickEventArgs e)
        {
            PageContext.RegisterStartupScript(window_view.GetShowReference(string.Format("LabEquipmentEdit.aspx?action=view&id={0}", Grid1.DataKeys[e.RowIndex][0].ToString()), string.Format("查看 - 资产编号：{0}", Grid1.DataKeys[e.RowIndex][1].ToString())));
        }

        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            if(e.CommandName == "Del")
            {
                int id = int.Parse(Grid1.DataKeys[e.RowIndex][0].ToString());
                if (BLL.LabEquipment.DelLabEquipmentByID(id))
                {
                    BindGrid();
                }
                else
                {
                    Alert.ShowInTop("删除失败！", "错误", MessageBoxIcon.Error);
                }
            }
        }
    }
}